package messages;

import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import utils.Serialization;
import utils.XmlSerializationUtils;
import connection.exception.ConnectionResetException;

public class SignatureRegisterRequestMessage implements Convertible {

    private static final long serialVersionUID = 1L;

    public Integer _signatureSerial;
    
    public String _signatureSignedValue;
    
    public Integer _sessionID;

    public SignatureRegisterRequestMessage() {}
     
    public SignatureRegisterRequestMessage(Integer serial, String signedValue) {
        _signatureSerial = serial;
        _signatureSignedValue = signedValue;
    }
    
    @Override
    public Element convertToCanonic(Document doc) {
        
        String className = getClass().getSimpleName();
        
        Element el = doc.createElement(className);

        Element childEl2 = doc.createElement("SignatureSerial");
        childEl2.setTextContent(_signatureSerial.toString());
        el.appendChild(childEl2);
        
        if (_signatureSignedValue != null) {
            Element childEl = doc.createElement("SignatureSignedValue");
            childEl.setTextContent(_signatureSignedValue);
            el.appendChild(childEl);

        }
        
        Element childEl1 = doc.createElement("SessionID");
        childEl1.setTextContent(_sessionID.toString());
        el.appendChild(childEl1);

        return el;
    } 


    @Override
    public Object convertFromCanonic(Element el) {
        this._signatureSerial = XmlSerializationUtils.getIntValue(el, "SignatureSerial");
        this._signatureSignedValue = XmlSerializationUtils.getTextValue(el, "SignatureSignedValue");
        this._sessionID = XmlSerializationUtils.getIntValue(el, "SessionID");
        return this;
    }

    public static void main(String[] args) throws ConnectionResetException, IOException{

        SignatureRegisterRequestMessage gen = new SignatureRegisterRequestMessage();
        gen._signatureSerial = 3;
        gen._signatureSignedValue = "SFDAFECcasdasdas";

        byte[] serialized = Serialization.Serialize(gen);

        Object newGen = Serialization.Deserialize(serialized);

        newGen.toString();

        System.in.read();
    }
}

